package com.mobileares.midp.widgets.client.list;

import com.google.gwt.event.logical.shared.SelectionEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-4-9
 * Time: 17:39:34
 * To change this template use File | Settings | File Templates.
 */
public class MultiListWidget extends ListWidget {
    private List<ListItem> selected = new ArrayList<ListItem>();


    void setSelected(ListItem selected, boolean fireSelectionEvent) {
        CheckBoxListItem item = (CheckBoxListItem) selected;
        boolean status = item.getCheckStatus();
        status = !status;
        item.setCheckStatus(status);
        if (status) {
            selected.addStyleDependentName("selected");
            if (this.selected.indexOf(selected) == -1) {
                this.selected.add(selected);
                if (fireSelectionEvent)
                    SelectionEvent.fire(this, item);
            }
        } else {
            selected.removeStyleDependentName("selected");
            this.selected.remove(selected);
        }


        return;
    }


    @Override
    public void addItem(ListItem item) {
        if (!(item instanceof CheckBoxListItem)) {
            item = new CheckBoxListItem(item.getUserObject());
        }
        super.addItem(item);
    }

    public List<ListItem> getSelectedItems() {
        return selected;
    }

    public ListItem getSelected() {
        throw new IllegalArgumentException("not support");
    }

    @Override
    public void removeAll() {
        super.removeAll();
        selected.clear();
    }
}
